from config.configs import GOAT_ERP_HOST, GOAT_ERP_PORT
from tools.funciton import write_xlsx, skuId_to_size, spuId_to_articleNumber, timestamp_switch_date, now_timestamp
import pymongo
import time

korea = pymongo.MongoClient(GOAT_ERP_HOST, GOAT_ERP_PORT).goat_warehouse_korea


def cancel_stock_out_order(outStockNo):
    """韩国仓取消指定出库单"""
    res = korea.stockoutorders.find_one({"outStockNo": outStockNo})
    sku = res['skuList']
    new = []
    now = now_timestamp()
    for i in sku:
        i['outQty'] = 0
        new.append(i)
    if res['status'] == 'delivered':
        korea.stockoutorders.update_one({"outStockNo": outStockNo}, {'$unset': {"pickedTime": 1, "deliverTime": 1},
                                                                     "$set": {"status": "cancelled", "cancelTime": now,
                                                                              "sku": new}})
    elif res['status'] == 'picked':
        korea.stockoutorders.update_one({"outStockNo": outStockNo}, {'$unset': {"pickedTime": 1},
                                                                     "$set": {"status": "cancelled", "cancelTime": now,
                                                                              "sku": new}})
    elif res['status'] == 'pendingPick':
        korea.stockoutorders.update_one({"outStockNo": outStockNo},
                                        {"$set": {"status": "cancelled", "cancelTime": now}})

# cancel_stock_out_order("CK2404086704281")
